package controllers

import (
	"github.com/kataras/iris"
	"github.com/kataras/iris/mvc"
	"tizhi_front/model"
	"tizhi_front/nspel"
	"tizhi_front/response"
)

type MessageController struct {
	Ctx iris.Context
}

func (c *MessageController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("POST", "/list/{offset:long}", "GetMessageList", beforeCommonToken)
	b.Handle("POST", "/un_read", "GetUnReadCount", beforeCommonToken)
	b.Handle("POST", "/read/{id:long}", "ReadMessage", beforeCommonToken)
}

// @Title Message List
// @Description 获取消息列表
// @Tags message
// @Param	body body response.TokenInfo true "token"
// @Success 0	{object} response.MessageList
// @Failure 1 {string} string "用户未登录或者登陆已过期"
// @Failure 2 {string} string "系统错误，获取失败，具体error信息为系统报错信息"
// @router /message/list/{offset} [post]
func (c *MessageController) GetMessageList(offset int64) response.JsonResponse {
	uid, err := c.Ctx.Values().GetInt64("uid")
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	message := model.MessageE{}
	count, err := message.SelectCountByStudentId(uid)
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	messages, err := message.SelectAllByStudentId(uid)
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	res := response.MessageList{
		PerRequestCount: 5,
		TotalCount:      count,
		MessageList:     []response.MessageListInfo{},
	}
	for i := int(offset); i < len(messages) && i-int(offset) < 5; i++ {
		res.MessageList = append(res.MessageList, response.MessageListInfo{
			Id:        messages[i].Id,
			Message:   messages[i].Message,
			IsRead:    messages[i].IsRead,
			CreatedAt: messages[i].CreatedAt.Format("2006-01-02 15:04:05"),
		})
	}
	return response.JsonResponse{
		ErrCode: 0,
		ErrInfo: "",
		Data:    res,
	}
}

// @Title Message UnRead Count
// @Description 获取未读消息数量
// @Tags message
// @Param	body body response.TokenInfo true "token"
// @Success 0	{object} response.JsonResponse
// @Failure 1 {string} string "用户未登录或者登陆已过期"
// @Failure 2 {string} string "系统错误，获取失败，具体error信息为系统报错信息"
// @router /message/un_read [post]
func (c *MessageController) GetUnReadCount() response.JsonResponse {
	uid, err := c.Ctx.Values().GetInt64("uid")
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	message := model.MessageE{}
	count, err := message.SelectCountByStudentIdIsRead(uid, false)
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	return response.JsonResponse{
		ErrCode: 0,
		ErrInfo: "",
		Data:    count,
	}
}

// @Title Message Read
// @Description 阅读消息
// @Tags message
// @Param	body body response.TokenInfo true "token"
// @Success 0	{object} response.JsonResponse
// @Failure 1 {string} string "用户未登录或者登陆已过期"
// @Failure 2 {string} string "系统错误，获取失败，具体error信息为系统报错信息"
// @router /message/read/{id} [post]
func (c *MessageController) ReadMessage(id int64) response.JsonResponse {
	uid, err := c.Ctx.Values().GetInt64("uid")
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	message := model.MessageE{}
	err = message.UpdateIsReadByIdStudentIdIsRead(id, uid, false)
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	return response.JsonResponse{
		ErrCode: 0,
		ErrInfo: "",
		Data:    "",
	}
}
